﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TestManagerClient.Model;

namespace TestManagerClient.Pages
{
    public partial class QuestionsPage : Page
    {

        public static List<Questions> QuestionsList = new List<Questions>();

        public QuestionsPage()
        {
            InitializeComponent();

            titleTextBlock.Text = "Pytania";

            InformationDisplay.HideNotification(notificationGrid, notificationTextBlock);
            InformationDisplay.HideWarning(warningGrid, warningTextBlock);
            InformationDisplay.HideError(errorGrid, errorTextBlock);

            btn_EditQuestions.IsEnabled = false;
            btn_DeleteQuestions.IsEnabled = false;

            QuestionsListBox.ItemsSource = QuestionsList.Select(p => p.Name);
        }

        #region Metody obsługujące przyciski zamknięcia informacji

        private void closeNotificationGridButton_Click(object sender, RoutedEventArgs e)
        {
            InformationDisplay.HideNotification(notificationGrid, notificationTextBlock);
        }

        private void closeWarningGridButton_Click(object sender, RoutedEventArgs e)
        {
            InformationDisplay.HideWarning(warningGrid, warningTextBlock);
        }

        private void closeErrorGridButton_Click(object sender, RoutedEventArgs e)
        {
            InformationDisplay.HideError(errorGrid, errorTextBlock);
        }

        #endregion

        private void Question_Selected(object sender, RoutedEventArgs e)
        {
            if (QuestionsListBox.SelectedItem != null)
            {
                btn_EditQuestions.IsEnabled = true;
                btn_DeleteQuestions.IsEnabled = true;
            }
            else
            {
                btn_EditQuestions.IsEnabled = false;
                btn_DeleteQuestions.IsEnabled = false;
            }
           
        }

        private void btn_AddQuestions_Click(object sender, RoutedEventArgs e)
        {
            //btn_AddQuestions.Click += new RoutedEventHandler(delegate(object senders, RoutedEventArgs es)
            //                                                           {
            //                                                               EditQuestion editQuestion = new EditQuestion();
            //                                                               editQuestion.Show();
            //                                                           });

            btn_AddQuestions.Command = NavigationCommands.GoToPage;
            btn_AddQuestions.CommandParameter = "Pages/EditQuestion1.xaml";
        }

        private void btn_EditQuestions_Click(object sender, RoutedEventArgs e)
        {
            if (QuestionsPage.QuestionsList.Count > 0)
            {
               var _question = QuestionsPage.QuestionsList.Where(p => p.Name == QuestionsListBox.SelectedValue).First();
               EditQuestion1 postModel = new EditQuestion1(_question);
               this.NavigationService.Navigate(postModel);
            }
        }

        private void btn_DeleteQuestions_Click(object sender, RoutedEventArgs e)
        {
            if (QuestionsPage.QuestionsList.Count > 0)
            {
                Questions c = QuestionsPage.QuestionsList.Where(p => p.Name == QuestionsListBox.SelectedValue).First();
                QuestionsPage.QuestionsList.Remove(c);

                QuestionsListBox.ItemsSource = null;
                QuestionsListBox.ItemsSource = QuestionsList.Select(p => p.Name);

                btn_DeleteQuestions.Command = NavigationCommands.GoToPage;
                btn_DeleteQuestions.CommandParameter = "Pages/QuestionsPage.xaml";
            }
            else QuestionsListBox.ItemsSource = null;
        }
    }
}
